package br.com.teste.manager.tarefa;

import br.com.teste.controller.tarefa.TarefaVo;
import br.com.teste.entity.Tarefa;
import br.com.teste.entity.Usuario;
import org.springframework.beans.factory.annotation.Autowired;

public class TarefaBuilder {

    @Autowired
    TarefaManager tarefaManager;

    public void build(TarefaVo vo) {

        Tarefa tarefa = null;
        if (vo.getId() == null) {
            tarefa = new Tarefa();
        } else {
            tarefa = tarefaManager.buscarPeloId(vo.getId());
        }

        tarefa.setNome(vo.getNome());
        tarefa.setDescricao(vo.getDescricao());
        tarefa.setDataInicio(vo.getDataInicio());
        tarefa.setDataFim(vo.getDataFim());
        
        if (vo.getUsuarioVo() != null) {
            Usuario u = new Usuario();
            u.setId(vo.getUsuarioVo().getId());
            u.setNome(vo.getUsuarioVo().getNome());
            tarefa.setUsuario(u);
        }
                                
        tarefaManager.salvar(tarefa);
    }
}
